package com.hmdp.jwt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class JsonUtils {
    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    public static String convertObj2Json(Object obj){
        return JSON.toJSONString(obj);
    }

    public static <T>T convertJson2Obj(String json,Class<T>classz){
        try{
            return JSONObject.parseObject(json,classz);
        }catch (Exception e){
            logger.error("convertJson2Obj异常.json:{}",json);
            return null;
        }
    }

    public static <T> List<T> convertJsonArray2List(String json,Class<T> classz){
        try{
            return JSONArray.parseArray(json,classz);
        }catch (Exception e){
            logger.error("convertJsonArray2List,json:{}",json,e);
            return null;
        }
    }
}